//
//  SINAServiceAgreementViewController.m
//  PlanTech
//
//  Created by  androidlongs on 2018/5/17.
//  Copyright © 2018年  androidlongs. All rights reserved.
//

#import "SINAServiceAgreementViewController.h"

@interface SINAServiceAgreementViewController ()<UIWebViewDelegate>

@property(strong,nonatomic) UIWebView *webView;

@end

@implementation SINAServiceAgreementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title=@"服务条款";
    self.view.backgroundColor=[UIColor colorWithHexString:@"#eeeeee"];
    
    [self initViewFunction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) initViewFunction{
    // 1.创建webview，并设置大小，"20"为状态栏高度
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, mainW, mainH)];
    // 2.创建URL
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",baseUrl,toUserprotocolUrl]];
    // 3.创建Request
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    // 4.加载网页
    [webView loadRequest:request];
    // 5.最后将webView添加到界面
    [self.view addSubview:webView];
    self.webView = webView;
     webView.delegate = self;
}


#pragma mark 设置前进后退按钮状态

/*浏览器后退*/
- (void)clickGoBackBtn{
    if(self.webView.canGoBack){
        [self.webView goBack];
    }
}
/*浏览器前进*/
- (void)clickGoForwardBtn{
    if(self.webView.canGoForward){
        [self.webView goForward];
    }
}
#pragma mark - UIWebViewDelegate代理方法
#pragma mark 开始加载
//是否允许加载网页，也可获取js要打开的url，通过截取此url可与js交互
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    //截取URL，这里可以和JS进行交互，但这里没有写，因为会涉及到JS的一些知识，增加复杂性
    NSString *urlString = [request.URL absoluteString];
    urlString = [urlString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
    DLog(@"urlString=%@---urlComps=%@",urlString,urlComps);
    return YES;
}
//开始加载网页
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //显示网络请求加载
    [UIApplication sharedApplication].networkActivityIndicatorVisible = true;
}
//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //隐藏网络请求加载图标
    [UIApplication sharedApplication].networkActivityIndicatorVisible = false;
    //[self setBarButtonStatus];
    //取得html内容
    DLog(@"%@",[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"]);
}
//网页加载错误
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"系统提示"
                                                    message:@"网络连接发生错误!"
                                                   delegate:self
                                          cancelButtonTitle:nil
                                          otherButtonTitles:@"确定", nil];
    [alert show];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
